/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pigx.admin.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.pig4cloud.pigx.admin.api.entity.MechanismDept;
import com.pig4cloud.pigx.admin.mapper.MechanismDeptMapper;
import com.pig4cloud.pigx.admin.service.MechanismDeptService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-20
 */
@Service
@AllArgsConstructor
public class MechanismDeptServiceImpl extends ServiceImpl<MechanismDeptMapper, MechanismDept> implements MechanismDeptService {


    private final MechanismDeptMapper deptMapper;


   /**
    *
    * @param
    * @return List<Tree<String>>
    * @author lfx
    * @date 2024/8/7 11:13
    * @Desc 查询机构部门树
    **/
    @Override
    public List<Tree<String>> selectTree() {
        // 查询全部部门
        List<MechanismDept> deptAllList = deptMapper.selectList(Wrappers.lambdaQuery(MechanismDept.class));
        List<TreeNode<String>> collect = deptAllList.stream()
                .filter(dept -> !dept.getDeptCode().equals(dept.getParentCode()))
                .sorted(Comparator.comparingInt(MechanismDept::getSortOrder)).map(dept -> {
                    TreeNode<String> treeNode = new TreeNode();
                    treeNode.setId(dept.getDeptCode());
                    treeNode.setParentId(dept.getParentCode());
                    treeNode.setName(dept.getDeptName());
                    treeNode.setWeight(dept.getSortOrder());
                    HashMap<String, Object> extra = Maps.newHashMap();
                    extra.put("description", dept.getDescription());
                    extra.put("dataId", dept.getId());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());
        return TreeUtil.build(collect, "0");
    }

    /**
     *
     * @param mechanismDept
     * @return void
     * @author lfx
     * @date 2024/8/7 11:13
     * @Desc 创建机构部门编码
     **/
    @Override
    public void createDeptCode(MechanismDept mechanismDept) {
//        if(StringUtils.isEmpty(mechanismDept.getParentCode())){
//            throw new RuntimeException("父级部门编码不能为空");
//        }
        String deptCode = deptMapper.selectMaxCode();
        mechanismDept.setDeptCode(String.valueOf(Integer.valueOf(deptCode) + 1));
    }

}
